﻿using System;
using System.Net.Http.Json;
using Compass.Wasm.Shared;
using Compass.Wasm.Shared.Charts;
using Compass.Wasm.Shared.Plans;
using Compass.Wasm.Shared.Projects;

namespace Compass.Wasm.Client.Services.Projects;
public interface IProjectService : IBaseService<ProjectDto>
{
    Task<ApiResponse<ProjectDto>> GetFirstOrDefaultByOdpNumberAsync(string odpNumber);
    Task<HttpResponseMessage> UploadFilesAsync(Guid id, ProjectDto dto);
    Task<List<ProjectDto>> GetBlazorBindAsync(string search);
    Task<List<ProjectDto>> GetBlazorProjectsAsync(int year, int month, bool annual, bool fromMonth);
    public Task<List<ChartIntDataDto>> GetBlazorProjectRiskLevelsAsync(int year, int month, bool annual);
    public Task<List<ChartIntDataDto>> GetBlazorProjectTypesAsync(int year, int month, bool annual);
    public Task<List<ChartIntDataDto>> GetBlazorProductDistributionAsync(int year);

}
public class ProjectService : BaseService<ProjectDto>, IProjectService
{
    private readonly HttpClient _http;
    private const string ServiceName = "Project";
    public ProjectService(HttpClient http) : base(http, ServiceName)
    {
        _http = http;
    }

    public Task<ApiResponse<ProjectDto>> GetFirstOrDefaultByOdpNumberAsync(string odpNumber)
    {
        return _http.GetFromJsonAsync<ApiResponse<ProjectDto>>($"api/{ServiceName}/OdpNumber?odpNumber={odpNumber}")!;
    }

    public Task<HttpResponseMessage> UploadFilesAsync(Guid id, ProjectDto dto)
    {
        return _http.PutAsJsonAsync($"api/{ServiceName}/UploadFiles/{id}", dto);
    }

    public async Task<List<ProjectDto>> GetBlazorBindAsync(string search)
    {
        var response = await _http.GetFromJsonAsync<ApiResponse<List<ProjectDto>>>($"api/{ServiceName}/Blazor/Bind?search={search}");
        return response.Status ? response.Result : new List<ProjectDto>();
    }
    
    public async Task<List<ProjectDto>> GetBlazorProjectsAsync(int year, int month, bool annual, bool fromMonth)
    {
        var response = await _http.GetFromJsonAsync<ApiResponse<List<ProjectDto>>>($"api/{ServiceName}/Blazor/All?year={year}&month={month}&annual={annual}&fromMonth={fromMonth}");
        return response.Status ? response.Result : new List<ProjectDto>();
    }
    //统计RiskLevels
    public async Task<List<ChartIntDataDto>> GetBlazorProjectRiskLevelsAsync(int year, int month, bool annual)
    {
        var response = await _http.GetFromJsonAsync<ApiResponse<List<ChartIntDataDto>>>($"api/{ServiceName}/Blazor/RiskLevel?year={year}&month={month}&annual={annual}");
        //查询失败则默认全部给0
        return response.Status ? response.Result : Enum.GetNames(typeof(RiskLevel_e)).Select(s => new ChartIntDataDto { Label = s, Value = 0 }).ToList();
    }
    public async Task<List<ChartIntDataDto>> GetBlazorProjectTypesAsync(int year, int month, bool annual)
    {
        var response = await _http.GetFromJsonAsync<ApiResponse<List<ChartIntDataDto>>>($"api/{ServiceName}/Blazor/Type?year={year}&month={month}&annual={annual}");
        //查询失败则默认全部给0
        return response.Status ? response.Result : Enum.GetNames(typeof(ProjectType_e)).Select(s => new ChartIntDataDto { Label = s, Value = 0 }).ToList();
    }

    public async Task<List<ChartIntDataDto>> GetBlazorProductDistributionAsync(int year)
    {
        var response = await _http.GetFromJsonAsync<ApiResponse<List<ChartIntDataDto>>>($"api/{ServiceName}/Blazor/ProductDistribution?year={year}");
        //查询失败则默认全部给0
        return response.Status ? response.Result : new List<ChartIntDataDto> { new() { Label = "", Value = 0 }};
    }
}